<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js 图片转base64方式</title>
</head>
<body>
<p id="container2"></p>

<div>
  <input type="text" id="originalInput" placeholder="Enter a string">
  <button onclick="encodeString()">Encode</button>
  <div id="encodedOutput"></div>
</div>
<div>
  <input type="text" id="encodedInput" placeholder="Enter a Base64 string">
  <button onclick="decodeString()">Decode</button>
  <div id="decodedOutput"></div>
</div>

<script>
  function encodeString() {
      let originalString = document.getElementById('originalInput').value;
      let encodedString = btoa(originalString);
      document.getElementById('encodedOutput').innerText = encodedString;
  }

  function decodeString() {
      let encodedString = document.getElementById('encodedInput').value;
      let decodedString = atob(encodedString);
      document.getElementById('decodedOutput').innerText = decodedString;
  }
</script>

  <input type="file" onchange="encodeImageFileAsURL(this)" />
  <script src="https://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>

  <script>

    console.log("触发了...")

    let inputStr = "";
    function change(val) {
      console.log("input value === ", val);
      inputStr = val;
    }

    function base64() {
      let originalString = "Hello, world!";
let encodedString = Buffer.from(originalString).toString('base64');
console.log(encodedString); 
    }

    function base64() {
      console.log("inputStr === ", inputStr);
      let originalString = inputStr;
      let encodedString = Buffer.from(originalString).toString('base64');
      document.getElementById("base64").innerHTML = encodedString;
      console.log(encodedString); 
    }

    function toDataURL(src, callback, outputFormat) {
      var img = new Image();
      img.crossOrigin = 'Anonymous';
      img.onload = function() {
        var canvas = document.createElement('CANVAS');
        var ctx = canvas.getContext('2d');
        var dataURL;
        canvas.height = this.naturalHeight;
        canvas.width = this.naturalWidth;
        ctx.drawImage(this, 0, 0);
        dataURL = canvas.toDataURL(outputFormat);
        console.log(dataURL);
        callback(dataURL);
      };
      img.src = src;
      if (img.complete || img.complete === undefined) {
        img.src = "";
        img.src = src;
      }
    }

    // toDataURL(
    //   './static/waimai.png',
    //   function(dataUrl) {
    //     console.log('RESULT:', dataUrl)
    //   }
    // )
  </script>

  <!-- <script>
    let imgSrc = "./static/waimai.png";
    function image2Base64(img) {
        var canvas = document.createElement("canvas");
        canvas.width = img.width;
        canvas.height = img.height;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, img.width, img.height);
        var dataURL = canvas.toDataURL("image/png");
        return dataURL;
    }

    function getImgBase64(){
        var base64="";
        var img = new Image();
        img.src="./static/waimai.png";
        img.onload=function(){
            base64 = image2Base64(img);
            alert(base64);
        }
    }

    getImgBase64();
  </script> -->
</body>
</html>